/*
 * Created on Jul 9, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

package com.herohead;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;

import org.apache.log4j.Logger;


/**
 * This class is used to store files to a particular directory.
 * 
 * If the filename already exists it will append the time to the end of the
 * file before saving
 */

public class Store {

	static Logger logger = Logger.getLogger(Store.class);

	String storeDir;
	String fileFilter;
	int maxCount = 0;

	public Store ()
	{
	}

	/**
	 * Set the store directory.
	 * @param dir
	 */

	public Store (String dir) {
		storeDir = dir;
	}
	/**
	 * Set the Store directory and filter
	 * @param dir
	 * @param filter
	 */
	public Store (String dir, String filter) {
		storeDir = dir;
		fileFilter = filter;
	}

	/**
	 * checkStoreSize checks the file and lowers the file count to the maxSize
	 * @param maxCount
	 */
	public void checkStoreSize() {

		Support sup = new Support();

		logger.info("Checking " + getDir() + " directory for " + getFilter() + "* for max number of " + maxCount);

		String[] fleList;
		try {
			fleList = sup.check4FileStartsWith(getDir(),getFilter());
			System.out.println(fleList.length);
			if (fleList.length > maxCount) {
				logger.info("Found total count of " + fleList.length + " when the max is " + maxCount + " deleting files as listed.");
				Arrays.sort(fleList);
				for (int i = 0; i < fleList.length - maxCount; i++) {
					File temp = new File(getDir() + "/" + fleList[i]);
					logger.info("Deleting " + fleList[i]);
					temp.delete();
				}
			} else {
				logger.info("No files to delete.");
			}
		} catch (Error e)
		{ logger.info(e);
		}
	}
	/**
	 * Get the store directory
	 * @return
	 */
	public String getDir() {
		return storeDir;
	}

	/**
	 * getFilter returns the fileFilter
	 * @return fileFilter
	 */
	public String getFilter() {
		return fileFilter;
	}
	/**
	 * Set Directory
	 * @param dir Store directory
	 * @throws IOException Directory not found
	 */

	public void setDir (String dir)
			throws IOException
			{
		File f = new File(dir);
		if (!f.isDirectory()){
			throw new IOException("Directory " + dir + " Not Found" );
		}
		storeDir = dir;
			}
	/**
	 * Set the file Fitler
	 * @param filter
	 */

	public void setFilter(String filter) {
		fileFilter = filter;
	}

	public void setMaxSize(String max) {
		maxCount = Integer.parseInt(max);
	}
	/**
	 * Create the file.
	 * 
	 * @param fle file in byte form
	 * @param fileName filename to store the bytes
	 */
	public void storeFile (byte[] fle, String fileName) {

		FileOutputStream fout = null;

		//Calendar cal = new GregorianCalendar();

		File f = new File(storeDir + "/" + fileName);

		if (f.exists()) {
			String cTime = (new SimpleDateFormat("hhmmss")).format(new java.util.Date());
			String tempName = fileName + "." + cTime;

			logger.warn("File already exists " + fileName + " storing as " + tempName);
			fileName = tempName;
		}

		try {
			fout = new FileOutputStream(storeDir + fileName);
			fout.write(fle);
			fout.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		logger.info("File " + fileName + " was saved to " + storeDir);

	}
}

